import 'package:flutter/material.dart';

import '../ordinary_pull_to_refresh.dart';

typedef pullWidgetBuilder = Future<bool> Function();

class SlideUpAndDownBox extends StatefulWidget {
  // 需要需要的列表组件
  Widget child;

  // 总长度
  int total;

  // 当前的数据长度
  int currentLen;

  // 是否开启下拉刷新
  bool openPullDown;

  // 是否开启上拉加载更多
  bool openPullUp;

  // 下拉刷新回调
  pullWidgetBuilder onPullRefresh;

  // 上拉刷新回调
  pullWidgetBuilder onPullLoadMore;

  /// 上下滑动组件（上拉加载更多，下拉刷新）
  /// * [child] Widget|展示的列表|必填
  /// * [total] int|总数据长度|必填
  /// * [currentLen] int|每次获取的数据长度|必填
  /// * [openPullDown] bool|是否开启下拉刷新|选填|默认值：false
  /// * [openPullUp] bool|是否开启上拉加载更多|选填|默认值：false
  /// * [onPullRefresh] pullWidgetBuilder|刷新的回调函数|选填
  /// * [onPullLoadMore] pullWidgetBuilder|加载更多的回调函数|选填
  SlideUpAndDownBox({
    @required this.child,
    @required this.total = 0,
    @required this.currentLen = 0,
    this.openPullDown = false,
    this.openPullUp = false,
    this.onPullRefresh,
    this.onPullLoadMore,
  });

  @override
  SlideUpAndDownBoxState createState() => new SlideUpAndDownBoxState();
}

class SlideUpAndDownBoxState extends State<SlideUpAndDownBox> {
  RefreshController refreshCtrl = RefreshController(initialRefresh: false);

  void _onRefresh() async {
    if (widget.openPullDown) {
      var _r = await widget.onPullRefresh();
      if (_r) {
        refreshCtrl.refreshCompleted();
      } else {
        refreshCtrl.refreshFailed();
      }
    }
  }

  /// 加载更多
  void _onLoading() async {
    if (widget.openPullUp) {
      if (widget.total > widget.currentLen) {
        var _r = await widget.onPullLoadMore();
        if (_r) {
          refreshCtrl.loadComplete();
        } else {
          refreshCtrl.loadFailed();
        }
      } else {
        refreshCtrl.loadNoData();
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 0),
      child: SmartRefresher(
        controller: refreshCtrl,
        enablePullDown: widget.openPullDown,
        enablePullUp: widget.openPullUp,
        header: WaterDropHeader(
          refresh: refreshWidget(),
          complete: completeWidget(),
          failed: failedWidget(),
        ),
        footer: CustomFooter(
          builder: (BuildContext crt, LoadStatus mode) => footerWidget(mode),
        ),
        onRefresh: widget.openPullDown ? _onRefresh : null,
        onLoading: widget.openPullUp ? _onLoading : null,
        child: widget.child,
      ),
    );
  }

  /// 下拉刷新回显组件（进行中）
  Widget refreshWidget() {
    return Container(
      alignment: Alignment.center,
      height: 50,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Spin(
            SPINTYPE.FADINGCUBE,
            style: SpinStyle(size: 20, color: Theme.of(context).primaryColor),
          ).build,
        ],
      ),
    );
  }

  /// 下拉完成回显组件（完成）
  Widget completeWidget() {
    return Container(
      width: MediaQuery.of(context).size.width,
      alignment: Alignment.center,
      height: 50,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(Icons.check, size: 20, color: Colors.black38),
          Text(
            "刷新成功",
            style: TextStyle(
              fontSize: 12,
              color: Colors.black38,
            ),
          ),
        ],
      ),
    );
  }

  /// 下拉刷新回显组件（失败）
  Widget failedWidget() {
    return Container(
      alignment: Alignment.center,
      height: 50,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(Icons.cancel, size: 20, color: Colors.black38),
          SizedBox(width: 5),
          Text(
            "刷新失败",
            style: TextStyle(
              fontSize: 12,
              color: Colors.black38,
            ),
          ),
        ],
      ),
    );
  }

  /// 底部状态组件
  Widget footerWidget(LoadStatus mode) {
    Widget _w;
    if (mode == LoadStatus.idle) {
      _w = Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(Icons.arrow_upward, size: 18, color: Colors.black38),
          // SizedBox(width: 5),
          Text(
            "上拉加载更多",
            style: TextStyle(
              fontSize: 12,
              color: Colors.black38,
            ),
          ),
        ],
      );
    } else if (mode == LoadStatus.loading) {
      _w = Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Spin(
            SPINTYPE.FADINGCIRCLE,
            style: SpinStyle(
              size: 22,
              color: Colors.black38,
            ),
          ).build,
          SizedBox(width: 5),
          Text(
            "正在加载中...",
            style: TextStyle(
              fontSize: 12,
              color: Colors.black38,
            ),
          ),
        ],
      );
    } else if (mode == LoadStatus.failed) {
      _w = Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(Icons.cancel, size: 20, color: Colors.black38),
          SizedBox(width: 5),
          Text(
            "加载失败 ！！！点击重试",
            style: TextStyle(
              fontSize: 12,
              color: Colors.black38,
            ),
          ),
        ],
      );
    } else if (mode == LoadStatus.canLoading) {
      _w = Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(Icons.touch_app, size: 20, color: Colors.black38),
          SizedBox(width: 5),
          Text(
            "松开触碰加载更多",
            style: TextStyle(
              fontSize: 12,
              color: Colors.black38,
            ),
          ),
        ],
      );
    } else {
      _w = Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            "没有更多数据了",
            style: TextStyle(
              fontSize: 12,
              color: Colors.black38,
            ),
          ),
        ],
      );
    }

    return Container(
      width: MediaQuery.of(context).size.width,
      // color: Colors.red,
      height: 60,
      child: _w,
    );
  }
}
